//
//  AMatchViewController.m
//  BigToBig
//
//  Created by 毛丽媛 on 16/8/5.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "AMatchViewController.h"
#import "MatchTimeCell.h"
#import "ChooseTableViewCell.h"
#import "AButtonTableViewCell.h"
#import "SZCalendarPicker.h"

@interface AMatchViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)BOOL IsSelected;
@property(nonatomic,strong)NSArray *titleArr;
@property(nonatomic,strong)NSArray *titTwoArr;
@property(nonatomic,strong)UIView *signUpView;
@property(nonatomic,strong)UITableView *signUpTableView;
@property (nonatomic , strong)NSMutableDictionary *contentDic;

@end

@implementation AMatchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(77, 155, 255, 1);
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 64)];
    titleLabel.text = @"发起速配";
    [titleLabel setFont:[UIFont systemFontOfSize:20]];
    titleLabel.textAlignment = 1;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    [self.navigationItem setTitleView:titleLabel];
    
    [self setBackBar];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = NO;
    self.tableView.bounces = NO;
    
    
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[ChooseTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.IsSelected = NO;
    
    [self.tableView registerClass:[MatchTimeCell class] forCellReuseIdentifier:@"time"];
    self.contentDic = [NSMutableDictionary new];

    [self createClickSignUp];
    
}

#pragma mark 点击报名创建View
- (void)createClickSignUp{
    self.signUpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.signUpView.backgroundColor = COLOR_RGBA(25, 26, 27, 0.8);
    
    self.signUpTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,kScreenHeight/2 - 70, kScreenWidth , 140)];
    self.signUpTableView.backgroundColor = [UIColor whiteColor];
    self.signUpTableView.delegate = self;
    self.signUpTableView.dataSource = self;
    
    [self.signUpView addSubview:self.signUpTableView];
    
    [self.signUpTableView registerClass:[ChooseTableViewCell class] forCellReuseIdentifier:@"SignUp"];
    [self.signUpTableView registerClass:[AButtonTableViewCell class] forCellReuseIdentifier:@"btn"];
    
    
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == self.tableView) {
        return 2;
    }else{
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableView) {
        if (section == 0) {
            if (self.IsSelected == YES) {
                return 3;
            }else{
                return 2;
            }
        }
        return 2;
    }
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (tableView == self.tableView) {
        return 25;
    }
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
            if (indexPath.row == 2) {
                return 120;
            }else{
                return 70;
            }
        }
        return 70;
    }
    return 70;
  
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    tableView.separatorStyle = YES;

    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
            
            if (indexPath.row == 0) {
                ChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.leftLab.text = @"速配日期";
                cell.rightLab.text = @"点击选择";
                return cell;
            }if (indexPath.row == 1) {
                ChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.leftLab.text = @"速配时间";
                cell.rightLab.text = @"点击选择";
                
                return cell;
            }
            MatchTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"time"];
            cell.selectionStyle = UITableViewCellAccessoryDetailButton;
            
            cell.time1.layer.borderColor = [COLOR_RGBA(217, 217, 217, 1) CGColor];
            
            [cell.time1 setTitleColor:[UIColor colorWithRed:0.267 green:1.000 blue:0.611 alpha:1.000] forState:UIControlStateSelected];
            
            cell.time2.layer.borderColor = [COLOR_RGBA(217, 217, 217, 1) CGColor];
            
            [cell.time2 setTitleColor:[UIColor colorWithRed:0.267 green:1.000 blue:0.611 alpha:1.000] forState:UIControlStateSelected];
            
            cell.time3.layer.borderColor = [COLOR_RGBA(217, 217, 217, 1) CGColor];
            
            [cell.time3 setTitleColor:[UIColor colorWithRed:0.267 green:1.000 blue:0.611 alpha:1.000] forState:UIControlStateSelected];
            
            cell.time4.layer.borderColor = [COLOR_RGBA(217, 217, 217, 1) CGColor];
            
            [cell.time4 setTitleColor:[UIColor colorWithRed:0.352 green:0.959 blue:0.552 alpha:1.000] forState:UIControlStateSelected];
            
            
            [cell.time1 addTarget:self action:@selector(time1:) forControlEvents:(UIControlEventTouchUpInside)];
            [cell.time2 addTarget:self action:@selector(time1:) forControlEvents:(UIControlEventTouchUpInside)];
            [cell.time3 addTarget:self action:@selector(time1:) forControlEvents:(UIControlEventTouchUpInside)];
            [cell.time4 addTarget:self action:@selector(time1:) forControlEvents:(UIControlEventTouchUpInside)];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
            
        }
        ChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLabel.text = self.titleArr[indexPath.row];
        
        self.titTwoArr = [[NSArray alloc] initWithObjects:@"感兴趣的行业",@"速配目标", nil];
        cell.leftLab.text = self.titTwoArr[indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray *aarr = [[NSArray alloc] initWithObjects:@"请选择时间",@"点击简单描述", nil];
        
        cell.rightLab.text = aarr[indexPath.row];
        return cell;
    }
        if (indexPath.row == 0) {
            ChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SignUp"];
            cell.leftLab.text = @"请选择秀券";
            cell.rightLab.text = @"速配秀券ID:XLUTMAB";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }
    AButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"btn"];
//    cell.sureBtn.frame = CGRectMake(kScreenWidth/2 - 100, 20, 200, cell.contentView.frame.size.height - 40);
    [cell.sureBtn setTitle:@"发起速配" forState:UIControlStateNormal];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
            if (indexPath.row == 0){
                SZCalendarPicker *calendarPicker = [SZCalendarPicker showOnView:self.view];
                calendarPicker.today = [NSDate date];
                calendarPicker.date = calendarPicker.today;
                calendarPicker.frame = CGRectMake(0, 100, self.view.frame.size.width, 352);
                calendarPicker.calendarBlock = ^(NSInteger day, NSInteger month, NSInteger year){
                    NSString *date = [NSString stringWithFormat:@"%ld-%ld-%ld", year,month,day];
                    [self.contentDic setObject:date forKey:@"createRoadShow-date"];
                    [tableView reloadData];
                };

            }
                
            if (indexPath.row == 1) {
                self.IsSelected = !self.IsSelected;
                [self.tableView reloadData];
                
            }
        }
    }
}

- (void)time1:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        btn.layer.borderColor = [COLOR_RGBA(151, 233, 198, 1) CGColor];
    }else{
        btn.layer.borderColor = [COLOR_RGBA(217, 217, 217, 1) CGColor];
    }
    
}

#pragma mark 导航栏左右Button
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    
    UIButton *saveBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(kScreenWidth - 44,0 , 44, 44);
    [saveBtn addTarget:self action:@selector(SaveMessage:) forControlEvents:UIControlEventTouchUpInside];
    
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setTitle:@"提交" forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:saveBtn];
    
}

- (void)SaveMessage:(UIButton *)btn{
    
    self.tabBarController.tabBar.hidden = YES;
    [self.view addSubview:self.signUpView];
    
}

#pragma mark 返回事件
- (void)backController:(UIButton *)btn{
    self.hidesBottomBarWhenPushed = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
